package com.scanner.hardware;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.scanner.ui.PublicTipsDialog;
import com.scanner.util.BaseUtils;
import com.scanner.util.CheckAppUpdateUtil;

public class AboutActivity extends AppCompatActivity {

	private CheckAppUpdateUtil appUpdateUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about);

		TextView tvApp =  findViewById(R.id.tv_app_version);
		findViewById(R.id.ll_back).setVisibility(View.VISIBLE);
		TextView tvAPpUpdateCode = findViewById(R.id.tv_AppUpdateCode);
		findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		tvApp.setText(String.format("v%s", BaseUtils.getAppVersionName(this)));


		findViewById(R.id.rl_check).setOnClickListener(v -> {
			appUpdateUtil = CheckAppUpdateUtil.getInstance(AboutActivity.this, true);
			appUpdateUtil.checkLastVersionFromService();
			appUpdateUtil.appDownSuccessListener(apkPath -> installProcess(apkPath));
		});

		tvAPpUpdateCode.setText(getString(R.string.app_update_code));

		findViewById(R.id.rl_Read_configuration).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				PublicTipsDialog dialog = PublicTipsDialog.getInstance(getString(R.string.sure_setting_read));
				dialog.setCancelText(getString(R.string.cancel));
				dialog.setSureText(getString(R.string.sure));
				dialog.addDialogOnClick(new PublicTipsDialog.OnDialogBtnClickListener() {
					@Override
					public void sureOnClick() {
						BaseUtils.beanToString(MyApplication.getApplication().xmlJiexi());
					}

					@Override
					public void cancelOnClick() {

					}
				});
				dialog.show(getSupportFragmentManager(),"dd");

			}
		});
	}

	private String apkPath;
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == GET_UNKNOWN_APP_SOURCES){
			installProcess(apkPath);
		}
	}

	//安装应用的流程
	public void installProcess(String apkFileString) {
		apkPath = apkFileString;
		boolean haveInstallPermission;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			//先获取是否有安装未知来源应用的权限
			haveInstallPermission = getPackageManager().canRequestPackageInstalls();
			if (!haveInstallPermission) {//没有权限
				startInstallPermissionSettingActivity();
				return;
			}
		}
		appUpdateUtil.install(apkFileString);
	}

	public static final int GET_UNKNOWN_APP_SOURCES = 12120;

	@RequiresApi(api = Build.VERSION_CODES.O)
	private void startInstallPermissionSettingActivity() {
		Uri packageURI = Uri.parse("package:" + getPackageName());
		//注意这个是8.0新API
		Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,packageURI);
		startActivityForResult(intent,GET_UNKNOWN_APP_SOURCES);
	}



}
